package edu.ncsu.csc.ip.common;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class NetworkUtils {


	
	public static InetAddress getLocalAdress(Socket socket) {
		return socket.getLocalAddress();
	}
	
	public static InetAddress getRemoteAdress(Socket socket) {
		return socket.getInetAddress();
	}


	@Deprecated
	public static String getIPAdress() {

		try {
			Enumeration<NetworkInterface> interfaces = NetworkInterface
					.getNetworkInterfaces();
			while (interfaces.hasMoreElements()) {
				NetworkInterface current = interfaces.nextElement();
				if (!current.isUp() || current.isLoopback()
						|| current.isVirtual())
					continue;
				Enumeration<InetAddress> addresses = current.getInetAddresses();
				while (addresses.hasMoreElements()) {
					InetAddress current_addr = addresses.nextElement();
					if (current_addr.isLoopbackAddress())
						continue;
					if (current_addr instanceof Inet4Address)
						return current_addr.getHostAddress();
				}
			}
		} catch (SocketException e) {
			throw new RuntimeException(e);
		}
		return "127.0.0.1";
	}

		
	public static String hostname() throws UnknownHostException{
		return InetAddress.getLocalHost().getHostAddress();
	}
	
	public static String getOsName() {
		return System.getProperty("os.name") +" " + System.getProperty("os.arch") +" " +System.getProperty("os.version");
	}
	
	
	

	
}
